import os
import sys
import sqlite3
import crypt

def create_auth(self, sender, flags, message, OWNER, SECRET, DB, DEBUG):
	message = message.split(' ')[1]
	 if DEBUG == True:
		print "in create_auth, message is %s" %(message)
		print "in create_auth, SECRET and OWNER are %s & %s" %(SECRET, OWNER)
	if sender.nickname == OWNER and message == SECRET:
		fp = crypt.crypt(sender.fingerprint, SECRET)
		 if DEBUG == True:
			print "fp = %s" %(fp)
		con=sqlite3.connect(DB)
		con.execute("DELETE FROM auth;")
		con.commit()
		con.execute("INSERT INTO auth VALUES (?, ?)", (sender.nickname, fp))
		con.commit()
		con.close()

		 if DEBUG == True:
			print "successfully authenticated, and stored fingerprint"
	else:
		 if DEBUG == True:
			print "failed to authenticate"

def check_auth(self, sender, DB, DEBUG, SECRET):
	con=sqlite3.connect(DB)
        cur=con.cursor()
	result = cur.execute("SELECT owner_fingerprint FROM auth WHERE owner_nick = '%s';" %(sender.nickname)).fetchone()[0]
	cur.close()
	print "crypt.crypt is %s and fp is %s" %(crypt.crypt(sender.fingerprint, SECRET), result)
	if crypt.crypt(sender.fingerprint, SECRET) == result:
		 if DEBUG == True:
			print "check_auth fingerprint result is %s" %(result)
		return True
	else:
		return False
